package com.example.mystore.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        String email = req.getParameter("email");
        String pwd = req.getParameter("password");
        String code = req.getParameter("check_code");
        if(!email.equals("") ){
            if (!pwd.equals("")){
                if (!code.equals("")){
                    if (code.equalsIgnoreCase((String) req.getSession().getAttribute("check_code"))){
                        filterChain.doFilter(req,resp);
                    }else {
                        req.getSession().setAttribute("err", "验证码错误");
                        resp.sendRedirect("/Gradle___com_example___mystore_1_0_SNAPSHOT_war/login.jsp");
                    }
                }else  {
                    req.getSession().setAttribute("err", "验证码不能为空");
                    resp.sendRedirect("/Gradle___com_example___mystore_1_0_SNAPSHOT_war/login.jsp");
                }
            }else  {
                req.getSession().setAttribute("err", "密码不能为空");
                resp.sendRedirect("/Gradle___com_example___mystore_1_0_SNAPSHOT_war/login.jsp");
            }
        }else  {
            req.getSession().setAttribute("err", "邮箱不能为空");
            resp.sendRedirect("/Gradle___com_example___mystore_1_0_SNAPSHOT_war/login.jsp");
        }
    }
}
